import { validTableName } from "@/utils/rules";
import type { FormRules } from "element-plus";

export type DefColumn = {
  tableId: string;
  columnKey: string;
  columnComment: string;
  ctrlType: string;
  convertConfig: DefColumnConvertConfig;
};

export type DefColumnConvertConfig = {
  columnKey: string;
  Type: string;
  targetTableId?: string;
  targetColumnKey: string;
  targetColumnComment?: object;
};

export const DefaultColumnList = [
  {
    id: "",
    columnName: "id",
    columnKey: "id",
    columnComment: "主键",
    dataType: "varchar",
    renderType: "Hidden",
    dataLength: 32,
    dataPrecision: 0,
    nullable: false,
    primaryKey: true,
    defaultValue: ""
  },
  {
    id: "",
    columnName: "flow_inst_id",
    columnKey: "flowInstId",
    columnComment: "流程实例ID",
    dataType: "varchar",
    dataLength: 32,
    dataPrecision: 0,
    nullable: true,
    primaryKey: false,
    renderType: "Hidden",
    defaultValue: ""
  },
  {
    id: "",
    columnName: "order_inst_id",
    columnKey: "orderInstId",
    columnComment: "工单实例ID",
    dataType: "varchar",
    dataLength: 32,
    dataPrecision: 0,
    nullable: true,
    primaryKey: false,
    renderType: "Hidden",
    defaultValue: ""
  }
];

export const DefColumnRules = {
  columnComment: [
    {
      required: true,
      message: "注释不能为空",
      trigger: "blur"
    }
  ],
  columnKey: [
    {
      required: true,
      message: "属性不能为空",
      trigger: "blur"
    },
    {
      validator: validTableName,
      trigger: "blur"
    }
  ],
  columnName: [
    {
      required: true,
      message: "数据库字段名不能为空",
      trigger: "blur"
    },
    {
      validator: validTableName,
      trigger: "blur"
    }
  ],
  dataLength: [
    {
      required: true,
      message: "属性长度不能为空",
      trigger: "blur"
    },
    {
      type: "number",
      message: "属性长度只能是数字",
      trigger: "blur"
    }
  ],
  dataType: [
    {
      required: true,
      message: "数据类型不能为空",
      trigger: "blur"
    }
  ],
  renderType: [
    {
      required: true,
      message: "渲染组件不能为空",
      trigger: "blur"
    }
  ],
  dataPrecision: [{ required: false, message: "请输入精度", trigger: "blur" }],
  defaultValue: [{ required: false, message: "请输入默认值", trigger: "blur" }]
} as FormRules;

export const DefTableRules = {
  tableShortName: [
    { required: true, message: "表名不能为空", trigger: "blur" },
    {
      validator: validTableName,
      trigger: "change"
    }
  ],
  tableTypeCode: [
    { required: true, message: "请选择所属分类", trigger: "blur" }
  ],
  dataSourceCode: [
    { required: true, message: "请选择数据源", trigger: "blur" }
  ],
  tableKey: [{ required: true, message: "请输入别名", trigger: "blur" }],
  tableComment: [{ required: true, message: "请输入描述", trigger: "blur" }]
} as FormRules;

export const dataSourceOptions = [
  {
    dataSourceId: "1",
    dataSourceName: "默认数据源",
    dataSourceCode: "default"
  }
];
export const dataTypeOptions = [
  { dataType: "varchar", dataTypeText: "字符串" },
  { dataType: "clob", dataTypeText: "大文本" },
  { dataType: "number", dataTypeText: "数字型" },
  { dataType: "date", dataTypeText: "日期型" }
];
export const renderTypeOptions = [
  { value: "Hidden", label: "隐藏" },
  { value: "CurrentUser", label: "当前人员" },
  { value: "CurrentDept", label: "当前部门" },
  { value: "UniqueCode", label: "唯一编码" },
  { value: "TextInput", label: "单行输入" },
  { value: "TextareaInput", label: "多行输入" },
  { value: "NumberInput", label: "数字输入" },
  { value: "SinglePicker", label: "单选" },
  { value: "MultiplePicker", label: "多选" },
  { value: "Score", label: "评分" },
  { value: "DateTimePicker", label: "日期时间" },
  { value: "DateTimeRangePicker", label: "日期时间范围" },
  { value: "UserPicker", label: "人员选择" },
  { value: "DeptPicker", label: "部门选择" },
  { value: "FileUpload", label: "文件上传" },
  { value: "DictPicker", label: "字典选择" },
  { value: "DefTableDataPicker", label: "实体表数据选择" }
];

export type TableListConverter = {
  columnKey: string;
  type: string;
  targetTableId: string;
  targetTableName: string;
  targetColumnKey: string;
  targetColumnComment: string;
};
